import { _decorator, Label, Node, Tween, tween } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import Config from '../../manager/config.manager';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
import { Guide } from '../page/guide';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Victory')
export class Victory extends BasePopUp<typeof Popup, null> {
	protected default_return: null;

	private lbl_round: Label;
	private lbl_symbol: Label;
	private spt_light: Node;
	private round: number;
	private symbol: number;

	protected initAfterOnLoad(){
		this.lbl_round = this.node.getChildByName('lbl-round').getComponent(Label);
		this.lbl_symbol = this.node.getChildByName('lbl-symbol').getComponent(Label);
		this.spt_light = this.node.getChildByName('light');
		this.renderLocalUI();
	}

	public onShow(round: number){
		this.round = round;
		this.symbol = GameDC.User.symbol;
		BasePopUp.jelly_enter(this.node);
		this.lbl_round.string = GameAssets.getLangText(19, round);
		this.lbl_symbol.string = Config.symbolString(GameDC.User.symbol);

		tween(this.spt_light)
		.set({angle: 0}) .to(5, {angle: -360}) .union()
		.repeatForever() .start();

		this.initGuide();

		this.guide_done.then(()=>{
			if(this.has_guide){
				return void 0;
			}

			mtec.delay(1.5, 'end')
			.then(()=>this.exit());
		});
	}

	private renderLocalUI(){
		(<const>[
			['head', 63],
			['desc', 61]
		]).forEach(([name, idx])=>{
			this.node.getChildByName('lbl-'+name).getComponent(Label).string = GameAssets.getLangText(idx);
		});
	}

	private async exit(){
		await BasePopUp.jelly_leave(this.node);
		Tween.stopAllByTarget(this.spt_light);
		this.spt_light.angle = 0;
		this.close(null);
	}

	private guide_done: Promise<'end'>;
	private has_guide: boolean = false;
	private initGuide(){
		let np = new mtec.NudityPromise<'end'>();
		this.guide_done = np.promise;
		if(GameDC.checkGuideID(6)){
			np.resolve('end');
			return void 0;
		}

		this.has_guide = true;

		GameDC.Trigger.HILIT_SYMBOL = true;

		Guide.Wait(5).then(()=>Guide.Show(6, 1))

		Guide.Wait(6, true).then(()=>{
			this.exit();
			GameDC.Trigger.HILIT_SYMBOL = false;
			np.resolve('end');
			this.has_guide = false;
		});
	}
}

